Skip to content

fix: unify duplicate-key error detection across routes#92

Merged
rubenhensen merged 1 commit into
mainfrom
fix/unique-violation-helper
May 25, 2026
Merged

fix: unify duplicate-key error detection across routes#92
rubenhensen merged 1 commit into
mainfrom
fix/unique-violation-helper

Conversation

@dobby-coder

@dobby-coder dobby-coder Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add isUniqueViolation(err) in src/lib/server/services/errors.ts — checks both message and cause.message (case-insensitive) for duplicate key or unique.
  • Replace the four inconsistent in-line checks with a call to the helper.
  • The portal members route previously only matched unique, so a postgres unique-violation surfacing only as duplicate key ... became a 500 instead of the intended 409. That gap is closed.

Test plan

  • npx vitest run — 60 passed (8 new in tests/unit/errors.test.ts)
  • npm run check — 0 errors, 0 warnings
  • npx vite build — succeeds (adapter-node bundling included)

Closes #89

Extracts the postgres unique-violation check into
src/lib/server/services/errors.ts so all four insert paths share the
same matcher. The members route previously only matched 'unique' and
missed the 'duplicate key' wording, which surfaced as a 500 to portal
users adding a duplicate-email member instead of the intended 409.

Closes #89

@dobby-coder dobby-coder Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — would approve if GitHub allowed self-approval on bot-authored PRs.

  • Helper at src/lib/server/services/errors.ts consolidates the four inconsistent in-line checks; case-insensitive haystack across message + cause.message matches the design from #89.
  • Members route gap is closed: previously only matched unique, now also catches duplicate key.
  • Verified no leftover ad-hoc checks in src/ (only schema .unique() constraints and a migration comment remain).
  • npx vitest run → 60/60 pass (8 new in tests/unit/errors.test.ts covering both wordings, cause-wrapping, case-insensitivity, plain {message} objects, and non-error inputs).
  • npm run check → 0 errors, 0 warnings. npm run lint → clean.

Minor (non-blocking): the register route's old check looked at err.stack ?? err.message. For standard Error objects stack always contains the message, so behaviour is equivalent in practice — flagging only in case a custom error class ever overrides stack.

@dobby-coder dobby-coder Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed (review cycle 1). Verdict from the prior review still stands: PR matches issue #89, all four call sites updated, members-route gap closed, helper handles Error/string/plain-object inputs case-insensitively over message + cause. 8 unit tests pass, CI green across Build/Lint/Svelte Check/Tests/Migration Safety. Cannot --approve (GitHub blocks self-review on bot-authored PRs); treating this as approval-equivalent — ready for a human merge.

@dobby-coder dobby-coder Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cycle 2: head 20e4b0c unchanged since cycle 1; re-verified tests (60/60), svelte-check (0/0), lint (clean), CI green. Verdict from prior reviews stands — approval-equivalent, ready for a human to merge. (Self-approve still blocked by GitHub.)

@rubenhensen rubenhensen merged commit f1bfad2 into main May 25, 2026
9 checks passed
@rubenhensen rubenhensen deleted the fix/unique-violation-helper branch May 25, 2026 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate-key error detection duplicated across 4 routes with inconsistent matching

1 participant